package com.bitinstitute.parkapp.animals.types;

import java.io.IOException;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.bitinstitute.parkapp.R;
import com.bitinstitute.parkapp.helper.ImageLoader;

public class TypesSingleMenuItemActivity extends Activity {

	// XML node keys
	static final String KEY_NAME = "name";
	static final String KEY_SCI = "sci_name";
	static final String KEY_ORIGIN = "origin";
	static final String KEY_FOOD = "food";
	static final String KEY_SIZE = "size";
	static final String KEY_WEIGHT = "weight";
	static final String KEY_INCUBATION = "incubation";
	static final String KEY_EGGS = "eggs";
	static final String KEY_AGE = "age";
	static final String KEY_PLACE = "place";
	static final String KEY_HEADING1 = "heading1";
	static final String KEY_DESC1 = "description1";
	static final String KEY_HEADING2 = "heading2";
	static final String KEY_DESC2 = "description2";
	static final String KEY_HEADING3 = "heading3";
	static final String KEY_DESC3 = "description3";
	static final String KEY_THUMB_URL = "thumb_url";
	static final String KEY_SOUND = "sound";
	static final String KEY_VIDEO = "video";

	MediaPlayer mediaPlayer = new MediaPlayer();
	ImageButton videoButton;
	ImageButton soundButton;

	@SuppressLint("NewApi")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		setContentView(R.layout.types_single_list_item);

		Intent in = getIntent();

		// Get XML values from previous intent
		String name = in.getStringExtra(KEY_NAME);
		String sci = in.getStringExtra(KEY_SCI);
		String origin = in.getStringExtra(KEY_ORIGIN);
		String food = in.getStringExtra(KEY_FOOD);
		String size = in.getStringExtra(KEY_SIZE);
		String weight = in.getStringExtra(KEY_WEIGHT);
		String incubation = in.getStringExtra(KEY_INCUBATION);
		String eggs = in.getStringExtra(KEY_EGGS);
		String age = in.getStringExtra(KEY_AGE);
		String place = in.getStringExtra(KEY_PLACE);
		String heading1 = in.getStringExtra(KEY_HEADING1);
		String desc1 = in.getStringExtra(KEY_DESC1);
		String heading2 = in.getStringExtra(KEY_HEADING2);
		String desc2 = in.getStringExtra(KEY_DESC2);
		String heading3 = in.getStringExtra(KEY_HEADING3);
		String desc3 = in.getStringExtra(KEY_DESC3);
		final String video_url = in.getStringExtra(KEY_VIDEO);

		// Displaying all values on the screen
		TextView lblName = (TextView) findViewById(R.id.name_label);
		TextView lblSci = (TextView) findViewById(R.id.sci_label);
		TextView lblOrigin = (TextView) findViewById(R.id.origin_label);
		TextView lblFood = (TextView) findViewById(R.id.food_label);
		TextView lblSize = (TextView) findViewById(R.id.size_label);
		TextView lblWeight = (TextView) findViewById(R.id.weight_label);
		TextView lblIncubation = (TextView) findViewById(R.id.incubation_label);
		TextView lblEggs = (TextView) findViewById(R.id.eggs_label);
		TextView lblAge = (TextView) findViewById(R.id.age_label);
		TextView lblPlace = (TextView) findViewById(R.id.place_label);
		TextView lblHeading1 = (TextView) findViewById(R.id.heading1_label);
		TextView lblDesc1 = (TextView) findViewById(R.id.desc1_label);
		TextView lblHeading2 = (TextView) findViewById(R.id.heading2_label);
		TextView lblDesc2 = (TextView) findViewById(R.id.desc2_label);
		TextView lblHeading3 = (TextView) findViewById(R.id.heading3_label);
		TextView lblDesc3 = (TextView) findViewById(R.id.desc3_label);

		lblName.setText(name);
		lblSci.setText(sci);
		lblOrigin.setText(origin);
		lblFood.setText(food);
		lblSize.setText(size);
		lblWeight.setText(weight);
		lblIncubation.setText(incubation);
		lblEggs.setText(eggs);
		lblAge.setText(age);
		lblPlace.setText(place);
		lblHeading1.setText(heading1);
		lblDesc1.setText(desc1);
		lblHeading2.setText(heading2);
		lblDesc2.setText(desc2);
		lblHeading3.setText(heading3);
		lblDesc3.setText(desc3);

		// Image which will be shown on top of page
		ImageView image = (ImageView) findViewById(R.id.header_picture);
		// Image url
		String image_url = in.getStringExtra(KEY_THUMB_URL);
		// ImageLoader class instance
		ImageLoader imgLoader = new ImageLoader(getApplicationContext());
		// url - image url to load
		// image - ImageView
		imgLoader.DisplayImage(image_url, image);

		// Url from mp3-soundfile
		String sound_url = in.getStringExtra(KEY_SOUND);

		// if sound_url is available then prepare soundfile
		if (sound_url.isEmpty() == false) {
			soundButton = (ImageButton) findViewById(R.id.sound_button);
			soundButton.setVisibility(View.VISIBLE);

			mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
			try {
				mediaPlayer.setDataSource(sound_url);
			} catch (IllegalArgumentException e) {
				Log.e("IllegalArgumentException", "error: " + e.getMessage(), e);
				mediaPlayer.release();
			} catch (SecurityException e) {
				Log.e("SecurityException", "error: " + e.getMessage(), e);
				mediaPlayer.release();
			} catch (IllegalStateException e) {
				Log.e("IllegalStateException", "error: " + e.getMessage(), e);
				mediaPlayer.release();
			} catch (IOException e) {
				Log.e("IOException", "error: " + e.getMessage(), e);
				mediaPlayer.release();
			}
			mediaPlayer.prepareAsync();

			// Sound Button
			findViewById(R.id.sound_button).setOnClickListener(
					new OnClickListener() {

						public void onClick(View v) {

							mediaPlayer.start();

						}
					});
		}

		// if video_url is available then show video_button
		if (video_url.isEmpty() == false) {
			// setting video from GONE to VISIBLE; Otherwise video will be
			// focused
			// on creating Activity
			videoButton = (ImageButton) findViewById(R.id.video);
			videoButton.setVisibility(View.VISIBLE);

			// Start new Intent when clicking on video image
			findViewById(R.id.video).setOnClickListener(new OnClickListener() {

				public void onClick(View v) {

					// Starting new intent
					Intent in = new Intent(getApplicationContext(),
							TypesVideoActivity.class);
					in.putExtra(KEY_VIDEO, video_url);

					startActivity(in);

				}
			});
		}

		// Back Button
		findViewById(R.id.btnBack).setOnClickListener(new OnClickListener() {

			public void onClick(View v) {

				dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
						KeyEvent.KEYCODE_BACK));

				dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,
						KeyEvent.KEYCODE_BACK));

			}
		});
	}

}
